package factorytest.iflytek.com.einkiflytekfactorytest;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import java.io.File;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import factorytest.iflytek.com.einkiflytekfactorytest.common.FTUtils;
import factorytest.iflytek.com.einkiflytekfactorytest.config.OTATestConfig;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.FileReadWrite;

public class OTATestActivity extends FullScreenBaseActivity implements View.OnClickListener {

    private TextView txtOtaFile;

    private View btnStart;

    private Context mContext;

    private String otaFilePath = null;

    private String OTA_POSTFIX = "_fullota.zip";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置窗体始终点亮
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_ota_test);

        mContext = this;

        txtOtaFile = findViewById(R.id.txtOtaFile);

        btnStart = findViewById(R.id.btnStart);
        btnStart.setOnClickListener(this);
        findViewById(R.id.btnExit).setOnClickListener(this);

        btnStart.setEnabled(false);

    }

    @Override
    protected void onResume() {
        super.onResume();
        searchOTAFile();
        if (!TextUtils.isEmpty(otaFilePath)) {
            btnStart.setEnabled(true);
            txtOtaFile.setText(getString(R.string.ota_test_file_path, otaFilePath));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        setResult(RESULT_OK);
        ActivityCompat.finishAfterTransition(this);
    }

    private void searchOTAFile() {

        String dir = FTUtils.getFTStorageDirectory();

        File dirFile = new File(dir);
        if (!dirFile.isDirectory()) {
            return;
        }

        for (File file : dirFile.listFiles()) {
            String name = file.getName();
            if (TextUtils.isEmpty(name)) {
                continue;
            }

            if (name.contains(OTA_POSTFIX)) {
                try {
                    otaFilePath = file.getCanonicalPath();
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnStart:
                doStart();
                break;
            case R.id.btnExit:
                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(this);
                break;
        }

    }

    private static final String COMMAND_FILE_PATH = "/cache/recovery/command";

    private void doStart() {

        if (TextUtils.isEmpty(otaFilePath)) {
            return;
        }

        OTATestConfig.setEssentialForOTATest(mContext, true);
        OTATestConfig.setOTAStarted(mContext, true);

        String updatePackage = "--update_package=" + otaFilePath;
        sendCommand(updatePackage + "\n");
        rebootRecovery();


    }

    private void sendCommand(String cmd) {
        FileReadWrite.writeStringToFile(COMMAND_FILE_PATH, cmd);
    }


    private static void rebootRecovery() {

        try {
            Runtime.getRuntime().exec("sync");
            Runtime.getRuntime().exec("sync");
            Runtime.getRuntime().exec("reboot recovery");

        } catch (Exception ex) {

        }
    }



}
